package com.hubu.jwxt.service;

import com.hubu.jwxt.beans.Course;
import com.hubu.jwxt.beans.SingleSubject;
import com.hubu.jwxt.beans.Student;
import com.hubu.jwxt.dao.CourseDAO;
import com.hubu.jwxt.dao.StudentDAO;
import com.hubu.jwxt.utils.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentDAO studentDAO;

    public PageHelper<SingleSubject> queryCourses(int pageNum, int pageSize, int Cid){
        //1、根据pageNum,pageSize计算从第几条开始查询
        int start = (pageNum-1)*pageSize;
        int num = pageSize;

        //2、调用courseDAO进行分页查询
        List<SingleSubject> list = studentDAO.selectByPage(start,num,Cid);

        //3、调用courseDAO查询学生总数
        int count = studentDAO.selectCount(Cid);

        //4、根据count和pageSize计算总页数
        int pageCount = count%pageSize==0 ? count/pageSize : count/pageSize+1;

        //封装分页数据
        PageHelper<SingleSubject> pageHelper = new PageHelper<>(count,pageNum,pageSize,pageCount,list);
        return pageHelper;
    }

    public int entryScore(String Sname,int Cid,String grade){
        int result = studentDAO.updateGrade(Sname,Cid,grade);
        return result;
    }
}
